package com.mchat.communication.process.frame.impl;

import com.mchat.communication.process.frame.FrameProcessor;
import com.mchat.communication.process.message.MsgProcessorFactory;
import com.mchat.communication.protocol.IMDecoder;
import com.mchat.communication.protocol.IMMessage;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class TextFrameProcessor implements FrameProcessor {
    @Resource
    private MsgProcessorFactory msgProcessorFactory;
    @Resource
    private IMDecoder decoder;


    @Override
    public void process(Channel channel, WebSocketFrame frame) {
        String msg = ((TextWebSocketFrame) frame).text();
        IMMessage message = decoder.decode(msg);
        msgProcessorFactory.getProcessor(message).process(channel, message);
    }
}
